﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjetoPeople.Areas.Administrativa.Models;
using ProjetoPeople.Models;
using System.IO;

namespace ProjetoPeople.Areas.Administrativa.Controllers
{
    public class NoticiaController : Controller
    {
        //
        // GET: /Administrativa/Noticia/
        PeopleTattooEntities banco = new PeopleTattooEntities();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Adicionar()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Adicionar(ViewNoticia n)
        {
            HttpPostedFileBase postedFile = null;
            foreach (string item in Request.Files)
            {
                postedFile = Request.Files[item];
            }

            byte[] buffer;
            Stream fs = postedFile.InputStream;


            try
            {
                int tamanho = (int)fs.Length;
                buffer = new byte[tamanho];
                int count = 0;
                int sum = 0;
                while ((count = fs.Read(buffer, sum, tamanho - sum)) > 0)
                {
                    sum += count;
                }
            }
            finally
            {
                fs.Close();

            }

            banco.noticias.AddObject(new noticia { noticia1= n.Noticia,capaid = Guid.NewGuid(), titulo = n.nomeImagem, capa = buffer });
            banco.SaveChanges();

            return View();
        }
    }
}
